package br.brpv.silvestrin.conferencia.dialogs;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import br.brpv.silvestrin.conferencia.R;
import br.brpv.silvestrin.conferencia.models.LoteOrigin;

public class LoteOriginEditDialog extends DialogFragment {
	
	// Use this instance of the interface to deliver action events
	private LoteOriginEditDialogListener mListener;	
	
	private EditText qtdeEditText;
	private EditText qtdeTotalEditText;
	private EditText confDescrEditText;
	
	private LoteOrigin lote;
	private EditText loteEditText;
	private EditText almoxEditText;
	private EditText codItemEditText;
	private EditText descrEditText;
	private EditText umEditText;
	
	// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (LoteOriginEditDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement LoteOriginEditDialogListener");
        }
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_edit_lote_origin, null);
        
        builder.setTitle(R.string.dialog_edit_lote_origin_title);
        
        builder.setView(dialogView);
        
        builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                	   int qtde;
                	   if(qtdeEditText.getText().toString().isEmpty()) qtde = 0;
                	   else qtde = Integer.parseInt(qtdeEditText.getText().toString());
                	   lote.setQtde(qtde);
                       mListener.onDialogEditLoteSave(lote);
                   }
               });
        builder.setNegativeButton(R.string.remove, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       dismiss();
                       mListener.onDialogEditLoteRemove(lote);
                   }
               });
        
        qtdeEditText = (EditText)dialogView.findViewById(R.id.edit_lote_qtde_editText);
        qtdeEditText.setText(lote.getQtde() + "");
        qtdeTotalEditText = (EditText)dialogView.findViewById(R.id.edit_lote_qtde_total_editText);
        qtdeTotalEditText.setText(lote.getQtdeTotal() + "");
        umEditText = (EditText)dialogView.findViewById(R.id.edit_lote_um_editText);
        umEditText.setText(lote.getUm());
        confDescrEditText = (EditText)dialogView.findViewById(R.id.edit_lote_conf_descr_editText);
        confDescrEditText.setText(lote.getDescrConfigurado());
        descrEditText = (EditText)dialogView.findViewById(R.id.edit_lote_descr_editText);
        descrEditText.setText(lote.getDescr());
        loteEditText  = (EditText)dialogView.findViewById(R.id.edit_lote_lote_number_editText);
        loteEditText.setText(lote.getCod());
        almoxEditText  = (EditText)dialogView.findViewById(R.id.edit_lote_almox_editText);
        almoxEditText.setText(lote.getAlmox());
        codItemEditText  = (EditText)dialogView.findViewById(R.id.edit_lote_item_cod_editText);
        codItemEditText.setText(lote.getCodItem());
        
        // Create the AlertDialog object and return it
        return builder.create();
    }

    public LoteOrigin getLote() {
		return lote;
	}

	public void setLote(LoteOrigin lote) {
		this.lote = lote;
	}

	public interface LoteOriginEditDialogListener {
        public void onDialogEditLoteSave(LoteOrigin lote);
        public void onDialogEditLoteRemove(LoteOrigin lote);
    }
}
